package com.icss.servlets;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.beans.PicVO;
import com.icss.daos.RollPicDao;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

public class RollPicServlet extends HttpServlet {

	public RollPicServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		RollPicDao  rpdao = new RollPicDao();
		if("showPic".equals(action)){
			List<PicVO> list =  rpdao.getAllPic();
			request.setAttribute("piclist", list);
			request.getRequestDispatcher("/page/index/rollPic/main.jsp").forward(request, response);
		}else if("show".equals(action)){
			request.getRequestDispatcher("/page/index/rollPic/show.jsp").forward(request, response);
		}else if("addPic".equals(action)){
			request.getRequestDispatcher("/page/index/rollPic/add.jsp").forward(request, response);
		}else if("add".equals(action)){
			String URL = getServletContext().getRealPath("/") + "img\\index\\gundongtiao\\";
			MultipartRequest mpr = new MultipartRequest(request, URL, 1024*1024*1024,"utf-8", new DefaultFileRenamePolicy());
			Enumeration params = mpr.getFileNames();
			String picTitle= mpr.getParameter("picTitle");
			String src=new String(mpr.getFilesystemName((String)params.nextElement()));
	
			
			PicVO p = new PicVO();
			p.setPicName(src);
			p.setPicTitle(picTitle);
			
			rpdao.addPic(p);
			request.getRequestDispatcher("RollPicServlet?action=showPic").forward(request, response);
		}else if("edit".equals(action)){
			int id = Integer.parseInt(request.getParameter("picID"));
			PicVO p = rpdao.getOnePic(id);
			request.setAttribute("picvo", p);
			request.getRequestDispatcher("/page/index/rollPic/edit.jsp").forward(request, response);
		}else if("update".equals(action)){
			int id = Integer.parseInt(request.getParameter("picID"));
			String URL = getServletContext().getRealPath("/") + "img\\index\\gundongtiao\\";
			
			MultipartRequest mpr = new MultipartRequest(request, URL, 1024*1024*1024,"utf-8", new DefaultFileRenamePolicy());
			
			String picTitle= mpr.getParameter("picTitle");
			String jj= mpr.getParameter("txt");
			if(jj.equals("")){
				rpdao.updatePic2(picTitle,id);
			}else{
				Enumeration params = mpr.getFileNames();
				String src=new String(mpr.getFilesystemName((String)params.nextElement()));
				PicVO p = new PicVO();
				p.setPicName(src);
				p.setPicTitle(picTitle);
				p.setPicID(id);
				rpdao.updatePic(p, URL);
			}
					
			request.getRequestDispatcher("RollPicServlet?action=edit").forward(request, response);
		}else if("del".equals(action)){
			int id = Integer.parseInt(request.getParameter("picID"));
			String URL = getServletContext().getRealPath("/") + "img\\index\\gundongtiao\\";
			rpdao.delPic(id,URL);
			request.getRequestDispatcher("RollPicServlet?action=showPic").forward(request, response);
		}else if("publish".equals(action)){
			int id = Integer.parseInt(request.getParameter("picID"));
			rpdao.publish(id);
			request.getRequestDispatcher("RollPicServlet?action=showPic").forward(request, response);
		}else if("dispublish".equals(action)){			
			int id = Integer.parseInt(request.getParameter("picID"));
			rpdao.dispublish(id);
			request.getRequestDispatcher("RollPicServlet?action=showPic").forward(request, response);
		}
		rpdao.close();
	}

	
	public void init() throws ServletException {
		// Put your code here
	}

}
